﻿using System.Linq;
using Wedn.Net.Common;
using Wedn.Net.Model;

namespace Wedn.Net.BLL
{
    public partial class PostManager
    {
        #region 获取文章前后 +Around<Post> GetPostAround(Post item)
        /// <summary>
        /// 获取文章前后
        /// </summary>
        /// <param name="item">当前文章</param>
        /// <returns></returns>
        public Around<Post> GetPostAround(Post item)
        {
            //var posts =
            //    DAO.Select(
            //        i =>
            //            i.Id == DAO.Select(p => p.Id > item.Id).Min(p => p.Id) ||
            //            i.Id == DAO.Select(p => p.Id < item.Id).Max(p => p.Id));
            //var prevQuery = DAO.Select(p => p.Id < item.Id && p.Type == (short)PostType.Article && p.Status >= (short)PostStatus.Default);
            //var nextQuery = DAO.Select(p => p.Id > item.Id && p.Type == (short)PostType.Article && p.Status >= (short)PostStatus.Default);
            //var prevId = prevQuery.Max(p => p.Id);
            //var nextId = nextQuery.Min(p => p.Id);
            var prev = DAO.Select(p => p.Id < item.Id && p.Type == (short)PostType.Article && p.Status >= (short)PostStatus.Default).OrderByDescending(p => p.Id).FirstOrDefault();
            var next = DAO.Select(p => p.Id > item.Id && p.Type == (short)PostType.Article && p.Status >= (short)PostStatus.Default).OrderBy(p => p.Id).FirstOrDefault();
            return new Around<Post>
            {
                Previous = prev,
                Next = next
            };
        } 
        #endregion
    }
}